<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Single;
use App\Admin\Category;
use App\Admin\GetCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class SingleController extends Controller
{
    //单品列表显示
    public function singleshow(Request $request){
        //将分类数据库表遍历到后台
        if($request->search){
            $search = $request->search;
            $searchlist =Single::where('name','like','%'.$search.'%');
            $result=$searchlist->paginate(5);
        }else{
            $Sing = Single::where([])->orderBy('category_id','asc');
            $result = $Sing->paginate(5);
        }
        return view('admin/single/SingleList',compact('result'));
    }
    //添加单品
    public function singleadd(){
        $Cate = Category::all();
        //new getTree方法
        $getCate = new GetCategory();
//        var_dump($getCate);
        $CateList= $getCate->getTree($Cate);

        return view('admin/single/SingleAdd',['CateList'=>$CateList]);
    }
    public function singleadddata(Request $request){
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
            'describe'=>'required|between:5,150',
            'visitor'=>'integer|alpha_num',
            'pic'=>'required',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '单品名称不能为空',
            'name.between'=>'单品名称必须在:min和:max之间',
            'describe.required' => '单品描述不能为空',
            'describe.between'=>'单品描述必须在:min和:max之间',
            'visitor.integer' => '访问量格式不对',
            'visitor.alpha_num'=>'访问量不能为负数',
            'pic.required'=>'单品图片不能为空',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/SingleAdd')->withErrors($validator);
        }
        $input = $request->all();
        $input['time']=time();
        //把传来的图片信息更改上传public/adminpublic/
        $input['pic']=time().'.jpg';
        $pic = $input['pic'];
//        dd(public_path().'/adminpublic/'.$pic);
        $request->pic->move(public_path() . '/adminpublic', $pic);
//        dd($input);
        $res = Single::create($input);
        return redirect('admin/SingleList');
    }
    //修改单品
    public function singleedit(){
        $search = isset($_GET['search'])?$_GET['search']:'';
        $page = $_GET['page'];
        $id = $_GET['id'];
        $Single = Single::where('id','=',$id)->get();
        $Sing = Category::all();
//        $Sing['page']=$page;
        $getSing = new GetCategory();
        $SingEdit= $getSing->getTree($Sing);
        return view('admin/single/SingleEdit',['Single'=>$Single,'SingEdit'=>$SingEdit,'page'=>$page,'search'=>$search]);
    }
    public function singleeditdata(Request $request,$page,$search=''){
        //接收要修改的ID
        $id = $request->id;
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
            'describe'=>'required|between:5,150',
            'visitor'=>'integer|alpha_num',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '单品名称不能为空',
            'name.between'=>'单品名称必须在:min和:max之间',
            'describe.required' => '单品描述不能为空',
            'describe.between'=>'单品描述必须在:min和:max之间',
            'visitor.integer' => '访问量格式不对',
            'visitor.alpha_num'=>'访问量不能为负数',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/SingleEdit?id='.$id.'&page='.$page)->withErrors($validator);
        }else{
            if($request->hasFile('pic')){
                $xiu =Single::find($id);
                $ppic=$xiu['pic'];

                @unlink(public_path().'/adminpublic'.'/'.$ppic);
                $inpu=$request->all();
                $inpu['pic']=time().'.jpg';
                $pic = $inpu['pic'];

                $request->pic->move(public_path() . '/adminpublic', $pic);
                $input = Input::except('_token');
                $input['pic']=$pic;
//                dd($input);
                $res = Single::where('id',$id)->update($input);
                return redirect('admin/SingleList?page='.$page.'&search='.$search);
            }else{
                $input = Input::except('_token');
                $res = Single::where('id',$id)->update($input);
                return redirect('admin/SingleList?page='.$page.'&search='.$search);
            }
        }
    }
//    删除单品
    public function singledel(Request $request){
        $search=$request->search;
        $id = $_GET['id'];
        $page=$_GET['page'];
        $Single = Single::find($id);
        @unlink(public_path().'/adminpublic'.'/'.$Single['pic']);
        $SingList = $Single->delete();
        if($search==''){
            $count = DB::table('single')->count();
//            dd($count);
        }else{
            $count = DB::table('single')
                ->where('name','like','%'.$search.'%')
                ->count();
        }
        $num = $count%5;
        if($num==0){
            $page= $page-1;
        }
        return redirect('admin/SingleList?page='.$page.'&search='.$search);
    }
}
